<script>
    $(function() {

        getTotalPrice();
        checkEmptycart();//测试是否清空购物篮
        checkMobileCart('{$is_mobile}');
        var plen = $('#activitylist').children('p').length;
        if (plen == 0) {
            $('#bonusArea').remove();
        }


    })
    function checkEmptycart() {
        var id = "{$Think.get.id}";
        var cartsid = $('#OrderBody').children("tr:first").attr("data-sid");

        if (cartsid > 0 && cartsid != id) {
            var div = "<div class='browsebox' id='browsebox' style='width:500px;'>\n\
    <div class='head' id='headbox'>\n\
        <a class='close' onclick=closebgbox('#mainbox')></a>\n\
        <span id='title'>请先清空美食筐</span></div>\n\
<div class='boxcontent' style='padding:0'>\n\
<div class='clear_cart_inner'>\n\
<div class='cart_food' style='padding:50px 0'><p class='cart_food_from'></p>\n\
<p>您需要清空美食筐才能挑选其他餐厅的美食哦~</p></div>\n\
<div class='do_clear_cart clearfix'>\n\
<a class='do_not_clear' onclick=closebgbox('#mainbox')>我先看看</a> <a class='do_clear' onclick=emptycart();closebgbox('#mainbox')>清空美食筐</a>\n\
</div></div></div></div>";
            $('#mainbox').html(div);
            showbgbox('#browsebox', 0, 0);
            $('#clearcart').show();

        }
    }
    function addToOrder(obj, id) {
        checkEmptycart();
        if ($('#addbtn').css("display") == 'block') {
            return false;
        }
        var price = obj.attr("data-price");
        $('#addbtn').val(price);
        if ($('#mainbox').html() != '') {
            return false;
        }
        var addTop = obj.offset().top;
        var addLeft = obj.offset().left;
        $('#addbtn').css({
            "left": addLeft,
            "top": addTop,
            "display": "block",
            "z-index": 9999
        })
        var siteLeft = $('#basketshow').offset().left;
        var siteTop = $('#basketshow').offset().top;
        var left = siteLeft - addLeft;
        var top = siteTop - addTop;
        $('#addbtn').animate({
            opacity: "1",
            left: "+=" + left + "",
            top: "+=" + top + ""
        }, 600, function() {
            $('#addbtn').fadeOut();
            addcart(obj, id);
        })
    }
    function addcart(obj, id) {
        if (!obj.hasClass('active')) {
            obj.addClass('active');
        }
        var name = obj.attr("data-name");
        var price = obj.attr("data-price");
        $.post("{:U('Cart/addToCart')}", {
            id: id,
            num: 1
        }, function(data) {

            if ($('#cart_' + id).length == 0) {
                var tr = "<tr id ='cart_" + id + "'><td><p class='food_name'>" + name + "</p>\n\
        <input type='hidden' name='id[]' value="+id+"></td>\n\
<td><div class='order_num'><a class='minus' onclick=changeNum(" + id + ",-1)> </a>\n\
<span id='num_" + id + "' class='num'>1</span>\n\
<a class='add' onclick=changeNum(" + id + ",1)> </a></div></td>\n\
<td class='price'>" + price + "</td>\n\
<td><a class='del' onclick=delcart($(this)," + id + ")>删除</a></td></tr>";
                $('#onlineOrder').find("tbody").append(tr);
            } else {
                var oldNum = parseInt($('#num_' + id).text());
                $('#num_' + id).text(oldNum + 1);
            }
            getTotalPrice();
        })
    }
    function changeNum(id, num) {

        var realnum = parseInt($("#num_" + id).text());
        if (num == -1 && realnum <= 1) {
            return false;
        }
        $.post("{:U('Cart/addToCart')}", {
            id: id,
            num: num
        }, function(data) {
            var oldNum = parseInt($('#num_' + id).text());
            $('#num_' + id).text(oldNum + num);
            getTotalPrice();
            if (oldNum + num <= 0) {
                $('#btn_' + id).removeClass("addMore");
                $('#num_' + id).parent('.order_num').parent('td').parent('tr').remove();
            }
        })

    }
    function delcart(obj, id) {
        $.post("{:U('Cart/addCut')}", {
            id: id
        }, function(data) {
            obj.parent("td").parent("tr").remove();
            getTotalPrice();
            $('#btn_' + id).removeClass("addMore");
        })
    }
    function emptycart() {
        $.post("{:U('Cart/emptyCart')}", {
        }, function(data) {

            $('#OrderBody').empty();
            getTotalPrice();
            $('.addToOrder').removeClass("addMore");
        })
    }
    function getTotalPrice() {
        var totalPrice = 0;
        $('#OrderBody').children("tr").each(function() {
            var price = $(this).find('.price').text();
            var num = $(this).find('.num').text();
            totalPrice += price * num;
        })
        $('#totalPrice').text(totalPrice.toFixed(1));
        if (totalPrice > 0) {
            $('#onlineOrder').slideDown(500);
            $('#no_send').hide();
            $('#tbbasket').show();
        } else {
            $('#onlineOrder').slideUp(500);
            $('#no_send').show();
            $('#tbbasket').hide();
        }
        var cartnum = 0;
        $("#OrderBody").find(".num").each(function() {
            cartnum += parseInt($(this).text());
        })
        $("#cartnum").text(cartnum);
    }
    function toBuy() {
        var startprice = "{$detail.startprice}";
        var totalPrice = parseInt($('#totalPrice').text());
        var div = ''
        if (totalPrice < startprice) {
            var url = $('#shop').attr("data-url");
            var name = $('#shop').text();
            div = "抱歉，<a style=color:blue href=" + url + ">" + name + "</a>起送价为{$detail.startprice}元";

        }
        if ($('#OrderBody').children("tr").length == 0) {
            div = "美食筐是空的！";
        }
        $(".cart_num").each(function(){
            var num = parseInt($(this).text());
            var input_num = "<input type='hidden' name='num[]' value='"+num+"'>";
            $(this).after(input_num);
        })
        if (div == '') {
            document.form_cart.submit();
        } else {
            $('#no_send_inner').html(div);
            $('#no_send').show();
            $('#onlineOrder').slideDown(200);
            $('#tbbasket').hide();
        }
    }
</script>
<form name="form_cart" method="POST" action="{:U('Cart/confirm')}">
    <div id="foodBasket">
        <div class="right_item shadow" id="onlineOrder" style=" border-top-color: #7db500;display: none">
            <table id="tbbasket" cellpadding="0" cellspacing="0" width="100%">
                <caption>美食筐</caption>
                <thead id="OrderHead">
                    <tr>
                        <th class="col1">菜品</th>
                        <th class="col2">份数 </th>
                        <th class="col3">单价</th>
                        <th class="col4">
                            <a href="javascript:void(0)" onclick="emptycart()">清除</a>
                        </th>
                    </tr>
                </thead>
                <tbody id="OrderBody">
                <foreach name="carts" item="row">
                    <tr data-sid="{$row.sid}" id="cart_{$row.id}">
                        <td><p class="food_name">{$row.name}</p><input type="hidden" name="id[]" value="{$row.id}"></td>
                        <td>
                            <div class="order_num">
                                <a class="minus" onclick="changeNum('{$row.id}', -1)"> </a>
                                <span class="num cart_num" id="num_{$row.id}">{$row.num}</span>
                                <a class="add" onclick="changeNum('{$row.id}', 1)"> </a>
                            </div>
                        </td>
                        <td class="price">{$row['price']-$row['discount']|getOneDot=###}</td>
                        <td><a class="del" onclick="delcart($(this), '{$row.id}')">删除</a></td>
                    </tr>
                </foreach>
                </tbody>
                <tfoot id="OrderFoot">
                    <tr class="last">
                        <td colspan="2" class="order_total"><p>总价</p></td>
                        <td><p id="totalPrice" class="order_price"></p></td>
                        <td></td>
                    </tr>
                </tfoot>
            </table>
            <div id="no_send" ><p id="no_send_inner"><empty name="carts">美食篮是空的</empty></p></div>
        </div>
        <div id="createOrder">
            <div class="shadow" style="height: 38px; background-color: #2c2c2c;">
                <i id="basketshow" class="fl" onclick="$('#onlineOrder').slideToggle(300)">
                    <img src="__PUBLIC__/images/no_food.png" width="28px" height="28px" />
                </i>
               
                    <a id="ComfirmOrder"  onclick="toBuy()"class="fr" >去买单> </a>
            </div>
        </div>
    </div>
</form>
<input class="addbtn" type="button" value="" id="addbtn" style="display: none">